home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 007 / cstrings.arc / INDEX.C < prev    next >
Text File  |  1985-08-06  |  3KB  |  139 lines

  1. /*
  2.     CSTRINGS.LBR VERSION 1.0
  3.     Spark Software, Inc.
  4.  
  5.         If you find this software of use, it is requested that you send
  6.         a donation ($10.00 suggested) to:
  7.  
  8.             Spark Software, Inc.
  9.             24 Royal Crest Dr., #5
  10.             Nashua, NH  03060
  11.  
  12.         Upon receiving your donation, your name will be added to the 
  13.         List of Registered Users, and future updates can be obtained
  14.         from the SPARKIE RBBS at (603) 888-8179.
  15.  
  16.         If you include an extra $10.00 with your donation, the newest
  17.         version of CSTRINGS.LBR will be mailed to you.
  18.  
  19.         Call SPARKIE RBBS at the number above for other Spark Software
  20.         products!!!
  21. */
  22.         
  23. /*
  24.  *    char *
  25.  *    index (str, c)
  26.  *    char *str;
  27.  *    int c;    /* just to allow c to be a register variable */
  28.  *
  29.  *    This function returns a pointer to the first occurrence of the
  30.  *    character c in string str.  It returns the NULL pointer if c does
  31.  *    not appear in str.
  32.  */
  33.  
  34. char *index(str, c)
  35. register char *str;
  36. register int c;
  37. {
  38.                     /* Loop through str */
  39.     while (*str) {
  40.  
  41.                     /* We found c, so return the
  42.                        current pointer */
  43.         if (*str == c)
  44.             return (str);
  45.  
  46.                     /* We did not find c, so keep
  47.                        looping */
  48.         ++str;
  49.     }
  50.  
  51.                     /* We never found c, so return
  52.                        the NULL pointer which is
  53.                        (almost) always defined as
  54.                        (char *) 0 */
  55.     return ((char *) 0);
  56.  
  57. } /* index */
  58.  
  59. /*
  60.  *    char *
  61.  *    rindex (str, c)
  62.  *    char *str;
  63.  *    int c;    /* just to allow c to be a register variable */
  64.  *
  65.  *    This function returns a pointer to the last occurrence of the
  66.  *    character c in string str.  It returns the NULL pointer if c does
  67.  *    not appear in str.
  68.  */
  69.  
  70. char *rindex(str, c)
  71. register char *str;
  72. register int c;
  73. {
  74.     register char *lastc;
  75.  
  76.                     /* First set up the default
  77.                        return value of NULL */
  78.     lastc = (char *) 0;
  79.  
  80.                     /* Now loop through str saving the
  81.                        latest occurence of c */
  82.     do {
  83.         if (*str == c)
  84.             lastc = str;
  85.     } while (*str++);
  86.  
  87.                     /* Finally return the last location
  88.                        of c (still NULL if c was never
  89.                        found in str */
  90.     return (lastc);
  91.  
  92. } /* rindex */
  93.  
  94. /*
  95.  *    char *
  96.  *    strchr (str, c)
  97.  *    char *str;
  98.  *    int c;    /* just to allow c to be a register variable */
  99.  *
  100.  *    This function returns a pointer to the first occurrence of the
  101.  *    character c in string str.  It returns the NULL pointer if c does
  102.  *    not appear in str.  It is simply to be compatible with some
  103.  *    compilers which use strchr/strrchr instead of index/rindex
  104.  */
  105.  
  106. char *strchr (str, c)
  107. register char *str;
  108. register int c;
  109. {
  110.  
  111.                     /* Just return what index
  112.                        would normally return */
  113.     return (index (str, c));
  114.  
  115. } /* strchr */
  116.  
  117. /*
  118.  *    char *
  119.  *    strrchr (str, c)
  120.  *    char *str;
  121.  *    int c;    /* just to allow c to be a register variable */
  122.  *
  123.  *    This function returns a pointer to the last occurrence of the
  124.  *    character c in string str.  It returns the NULL pointer if c does
  125.  *    not appear in str.  It is simply to be compatible with some
  126.  *    compilers which use strchr/strrchr instead of index/rindex.
  127.  */
  128.  
  129. char *strrchr (str, c)
  130. register char *str;
  131. register int c;
  132. {
  133.  
  134.                     /* Just return what rindex
  135.                        would normally return */
  136.     return (rindex (str, c));
  137.  
  138. } /* strrchr */
  139.